package com.kun.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kun.mall.coupon.entity.SeckillSkuRelationEntity;
import com.kun.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.mall.coupon.dao.SeckillSessionDao;
import com.kun.mall.coupon.entity.SeckillSessionEntity;
import com.kun.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3Session() {
        Map<String, String> timeRange = getTimeRange();

        LambdaQueryWrapper<SeckillSessionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SeckillSessionEntity::getStartTime,timeRange.get("startTime"),timeRange.get("endTime"));
        List<SeckillSessionEntity> list = this.list(wrapper);

        //查询活动相关商品
        if (list != null && list.size() > 0){

            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                LambdaQueryWrapper<SeckillSkuRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SeckillSkuRelationEntity::getId,session.getId());
                List<SeckillSkuRelationEntity> relationEntityList = seckillSkuRelationService.list(queryWrapper);
                session.setRelationSkus(relationEntityList);
                return session;
            }).collect(Collectors.toList());

            return collect;
        }
        return null;
    }

    private Map<String,String> getTimeRange(){

        HashMap<String, String> map = new HashMap<>();

        LocalDate now = LocalDate.now();
        LocalDate nowP2 = now.plusDays(2);

        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;

        LocalDateTime start = LocalDateTime.of(now, min);
        LocalDateTime end = LocalDateTime.of(nowP2, max);

        String startTime = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endTime = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        map.put("startTime",startTime);
        map.put("endTime",endTime);
        return map;
    }

}